import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {ZBar, ZBarOptions} from "@ionic-native/zbar";
import {Settings} from "../../pages";
import {AppUrl, STORAGE_KEY} from "../../../app/app.general";
import {Storage} from "@ionic/storage";
import {AppConfig} from "../../../app/appConfig";
import {HttpClient} from "@angular/common/http";
import {StaffShowPage} from "../busi-staff-mng/staff-show/staff-show";

/**
 * Generated class for the MyPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-my',
  templateUrl: 'my.html',
})
export class MyPage {
  private pageMenu = {
    title: '我的',
    userName: '正在获取',
    bossMenu: [
      {title: '我的店铺', icon: 'analytics', color: '#ee0000'},
      {title: '员工管理', icon: 'contacts', color: '#f807ff'},
      {title: '扫一扫', icon: 'analytics', color: 'black'},
      {title: '生成桌面二维码', icon: 'analytics', color: '#66ccff'},
      {title: '员工管理', icon: 'analytics', color: '#66ccff'},
      {title: '支付条码扫描', icon: 'analytics', color: 'black'},
      {title: '手机支付接口调用', icon: 'analytics', color: 'black'},
      {title: '统计', icon: 'moon', color: 'blue'},

    ],
    adminMenu: [
      //超级管理员菜单
      {title: '商户管理', icon: 'briefcase', color: '#66ccff'},
      {title: '账户管理', icon: 'finger-print', color: '#66ccff'},
    ]
  };
  /**
   * 此处用于日期选择回调
   */
  private callBackData;
  getData = (data) => {
    return new Promise((resolve) => {
      this.callBackData = data;
      console.log(this.callBackData);
      resolve();
    });
  };

  //回调结束 调用日期选择时传入该回调函数即可
  constructor(public navCtrl: NavController, public navParams: NavParams, private zbar: ZBar, private storage: Storage, private appConfig: AppConfig, private http: HttpClient) {
  }

  ionViewDidLoad() {
    setTimeout(() => {
      this.storage.get(STORAGE_KEY.userName).then(res => {
        if (res)
          this.pageMenu.userName = res;
        else this.pageMenu.userName = '获取失败';
      });
    }, 2000);
  }

  pagePush(title) {
    console.log(title);
    switch (title) {
      case '订单管理':
        this.navCtrl.push('MerManageTransPage');
        break;
      case '扫一扫':
        this.scan();
        break;
      case '商品管理':
        this.navCtrl.push('MerManageGoodsManagePage');
        break;
      case '生成桌面二维码':
        this.navCtrl.push('TableQrPage');
        break;
      case '商户管理':
        this.navCtrl.push('BusinessManagePage');
        break;
      case '账户管理':
        this.navCtrl.push('AccountManagePage');
        break;
      case '我的店铺':
        this.navCtrl.push('MyRestaurantPage');
        break;
      case '员工管理':
        this.navCtrl.push('StaffShowPage');
        break;
      case '支付条码扫描':
        this.scanToPay();
        break;
      case '统计':
        this.navCtrl.push('BusinessReportPage');
        break;
      case '手机支付接口调用':
        let rnd = "";
        for (let i = 0; i < 20; i++)
          rnd += Math.floor(Math.random() * 10);
        this.http.post(AppUrl.aliPay.alipayH5, {
          out_trade_no: rnd, //此处填写我们的订单号，我随机生成了20位进行测试
          subject: '餐桌105点餐',
          total_amount: '400',
          description: '鸭掌x5,西湖牛肉羹,粉丝汤'
        }).subscribe(res => {
          let temp: any = res;
          document.getElementById('form-hidden').innerHTML = temp.data;
          document.forms[0].submit();
        }, reject => {
         this.appConfig.presentToast(reject);
        });
        break;
    }
  }

  scan() {
    let options: ZBarOptions = {
      flash: 'auto',
      text_title: '扫一扫',
      drawSight: true,
      text_instructions: "扫描条形码/二维码"
    };

    this.zbar.scan(options)
      .then(result => {
        let temp = result.split('!@#');
        let type;
        let data;
        if (temp.length > 1) {
          type = temp[0];
          data = temp[1];
        }
        else
          data = temp[0];

        if (type == '999')
          this.navCtrl.push('ConfirmOrderPage', {data: data});
        else
          this.appConfig.presentAlert('未识别的二维码', '二维码内容:' + data, '我知道了');
      })
      .catch(error => {
        this.appConfig.presentToast(error);
      });
  }

  scanToPay() {
    let options: ZBarOptions = {
      flash: 'auto',
      text_title: '扫码支付',
      drawSight: true,
      text_instructions: "扫描支付宝二维码"
    };

    this.zbar.scan(options)
      .then(result => {
        this.http.post(AppUrl.aliPay.barcodeBC, {
          subject: '满汉全席',
          description: '无描述',//此处不需填写，因为写了也不会显示
          amount: '2',
          authCode: result
        }).subscribe(res => {
          let temp: any = res;
          alert(temp.data.toString());
        }, reject => {
          this.appConfig.presentToast(reject);
        });
      })
      .catch(error => {
        this.appConfig.presentToast(error);
      });
  }

  showSettings() {
    this.navCtrl.push(Settings);
  }
}
